package com.ruoyi.exam.serive.impl;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.vo.PageResult;
import com.ruoyi.course.domain.SysCourse;
import com.ruoyi.course.mapper.SysCourseMapper;
import com.ruoyi.exam.domain.ExamInfo;
import com.ruoyi.exam.domain.ExamTheory;
import com.ruoyi.exam.domain.Scores;
import com.ruoyi.exam.enums.ExamStatusEnum;
import com.ruoyi.exam.mapper.ExamInfoMapper;
import com.ruoyi.exam.mapper.ExamTheoryMapper;
import com.ruoyi.exam.mapper.ScoresMapper;
import com.ruoyi.exam.serive.IExamScoreService;
import com.ruoyi.exam.serive.IExamService;
import com.ruoyi.exam.serive.IScoresService;
import com.ruoyi.exam.vo.*;
import com.ruoyi.questionBank.domain.AnswerResult;
import com.ruoyi.questionBank.service.IExamPaperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ScoresServiceImpl implements IScoresService {

    @Autowired
    private ScoresMapper scoresMapper;
    @Autowired
    private SysCourseMapper courseMapper;

    @Override
    public void insertScores(Scores scores) {
        scoresMapper.insertScores(scores);
    }

    @Override
    public void updateScores(Scores scores) {
        scoresMapper.updateScores(scores);
    }

    @Override
    public List<Scores> listScores(ScoresVO scoresVO) {
        return scoresMapper.listScores(scoresVO);
    }

    @Override
    @Transactional
    public void real(ExamRealVO examRealVO) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ScoresVO scoresVO = new ScoresVO();
        scoresVO.setExamPaperId(0);
        scoresVO.setType(1);
        scoresVO.setUserId(loginUser.getUserId());
        List<Scores> scoresList = scoresMapper.listScores(scoresVO);
        if (CollectionUtils.isEmpty(scoresList)) {
            Scores scores = new Scores();
            scores.setScores(examRealVO.getScores());
            scores.setCreateTime(new Date());
            scores.setExamPaperId(0);
            scores.setType(1);
            scores.setUpdateTime(scores.getCreateTime());
            scores.setUserId(loginUser.getUserId());
            scores.setExamCount(1);
            scoresMapper.insertScores(scores);
        } else {
            scoresList.get(0).setScores(examRealVO.getScores());
            scoresMapper.updateScores(scoresList.get(0));
        }
    }

    @Override
    public List<ScoresCourseVO> selectScoresCourseList(ScoresCourseVO scoresCourseVO) {
        return scoresMapper.selectScoresCourseList(scoresCourseVO);
    }

    @Override
    public List<ScoresCourseTypeVO> selectScoresCourseTypeList(ScoresCourseTypeVO scoresCourseVO) {
        List<ScoresCourseTypeVO> list = scoresMapper.selectScoresCourseTypeList(scoresCourseVO);
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }
        //在除以课程数量
        List<SysCourse> courses = courseMapper.selectCourseCountByType(scoresCourseVO.getDelFlag(),scoresCourseVO.getReleaseStatus());

        list.forEach(item -> {
            if (!CollectionUtils.isEmpty(courses)) {
                Long courseCount = 0L;
                for (SysCourse course : courses) {
                    if (item.getCourseType() != null && course.getCourseType() != null &&
                            item.getCourseType().intValue() == course.getCourseType().intValue()) {
                        courseCount = course.getCourseId(); //暂时用courseId当统计字段
                        break;
                    }
                }
                //
                if (courseCount <= 0) {
                    item.setScores(0L);
                } else {
                    item.setScores(item.getScores()/courseCount);
                }
            }
        });

        return list;
    }

    @Override
    public List<ScoresRealVO> selectScoresRealList(ScoresRealVO scoresRealVO) {
        scoresRealVO.setType(1);
        scoresRealVO.setPaperId(0L);
        return scoresMapper.selectScoresRealList(scoresRealVO);
    }

    @Override
    public List<ScoresTheoryVO> selectScoresTheoryList(ScoresTheoryVO scoresTheoryVO) {
        scoresTheoryVO.setType(2);
        return scoresMapper.selectScoresTheoryList(scoresTheoryVO);
    }

    @Override
    public int deleteCourseStudent(Long userId, Long courseNo) {
        return scoresMapper.deleteCourseStudent(userId,courseNo);
    }

    @Override
    public int deleteScoresTheory(Long userId, Long paperId) {
        return scoresMapper.deleteScoresTheory(userId,paperId);
    }
}
